package com.szmirren.entity.ams;

import com.szmirren.common.StringUtil;

import java.sql.Types;

/**
 * @author Lin
 * @date 2018/5/26
 */
public class Column {
    private String name;
    private int type;
    private String typeName;
    private int length;
    private int floatLength;
    private String comment;
    private boolean nullable;

    private String javaType;
    private String dataType;
    private String boType;
    private String firstUpperName;

    private boolean primaryKey;


    public Column(String name, int type, String typeName, int length, int floatLength, String comment, boolean nullable, boolean pk) {
        name = name.toUpperCase();
        this.name = name;
        this.type = type;
        this.typeName = typeName;
        this.length = length;
        this.floatLength = floatLength;
        this.comment = comment;
        this.nullable = nullable;
        this.primaryKey = pk;

        switch (type) {
            case Types.BINARY:
            case Types.SMALLINT:
            case Types.INTEGER:
                this.javaType = "int";
                break;
            case Types.BIGINT:
                this.javaType = "long";
                break;
            case Types.DATE:
            case Types.TIMESTAMP:
                this.javaType = "Timestamp";
                this.dataType = "DateTime";
                break;
            case Types.NUMERIC:
            case Types.DECIMAL:
                if (floatLength > 0) {
                    this.javaType = "double";
                } else if (length <= 6) {
                    this.javaType = "int";
                } else {
                    this.javaType = "long";
                }
                break;
            default:
                this.javaType = "String";
        }
        if (dataType == null) {
            dataType = Character.toUpperCase(this.javaType.charAt(0)) + this.javaType.substring(1);
        }
        this.boType = this.dataType;
        if ("Int".equals(this.boType)) {
            this.boType = "Integer";
        }

        String[] arr = StringUtil.split(name, '_');
        StringBuilder sb = new StringBuilder();
        for (String s : arr) {
            if (s.length() == 1) {
                sb.append(s);
            } else {
                sb.append(s, 0, 1).append(s.toLowerCase().substring(1));
            }
        }
        this.firstUpperName = sb.toString();
    }

    public String getName() {
        return name;
    }

    public int getType() {
        return type;
    }

    public String getTypeName() {
        return typeName;
    }

    public int getLength() {
        return length;
    }

    public int getFloatLength() {
        return floatLength;
    }

    public String getComment() {
        return comment == null ? "" : comment;
    }

    public boolean isNullable() {
        return nullable;
    }

    public String getFirstUpperName() {
        return firstUpperName;
    }

    public String getJavaType() {
        return javaType;
    }

    public String getDataType() {
        return dataType;
    }

    public String getBoType() {
        return boType;
    }

    public boolean isPrimaryKey() {
        return primaryKey;
    }

    public String getColType() {
        return primaryKey ? "PK" : "COL";
    }
}
